<!-- 渠道信息 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="渠道标识">
                            <a-input v-model="channelTag" allow-clear placeholder="请输入渠道标识" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="公司名称">
                            <a-input v-model="companyName" allow-clear placeholder="请输入公司名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="银行名称">
                            <a-input v-model="bankName" allow-clear placeholder="请输入银行名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="账号">
                            <a-input v-model="account" allow-clear placeholder="请输入账号" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="开户行">
                            <a-input v-model="openingBank" allow-clear placeholder="请输入开户行" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="状态">
                            <a-select v-model="status" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="下单方式">
                            <a-select v-model="orderMode" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in orderModeList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button type="primary" style="margin-right: 20px" @click="getData()">查询</a-button>
                            <a-button type="" icon="plus" @click="add()">添加</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size, showTotal: (total) => `交易笔数：${total}条`}" size="small" :scroll="{ x: 1500, }">
            <div slot="minRate" slot-scope="minRate">
                <div>{{(minRate * 100).toFixed(2)}}%</div>
            </div>
            <div slot="costRate" slot-scope="costRate">
                <div>{{(costRate * 100).toFixed(2)}}%</div>
            </div>
            <div slot="orderMode" slot-scope="orderMode">
                <div v-for="(item,i) in orderModeList" :key="item.id">
                    <a-tag v-if="item.id == orderMode" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="isWaitSubmit" slot-scope="isWaitSubmit">
                <div v-for="(item,i) in isWaitSubmitList" :key="item.id">
                    <a-tag v-if="item.id == isWaitSubmit" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="status" slot-scope="status">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="status == '1' ? true : false" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <a-button type="link" @click="update(record)">修改</a-button>
                <a-popconfirm
                    title="是否确认删除当前配置?"
                    ok-text="Yes"
                    cancel-text="No"
                    @confirm="del(record)"
                >
                    <a-button type="link">删除</a-button>
                </a-popconfirm>
            </div>
        </a-table>

        <a-modal
            title="添加、修改渠道信息"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="公司名称">
                        <a-input v-model="item.companyName" placeholder="请输入公司名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="渠道标识">
                        <a-input v-model="item.channelTag"  placeholder="请输入渠道标识" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="银行名称">
                        <a-input v-model="item.bankName" placeholder="请输入银行名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="账户">
                        <a-input v-model="item.account"  placeholder="请输入账户" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="开户行">
                        <a-input v-model="item.openingBank" placeholder="请输入开户行" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="外放最低费率">
                        <a-input v-model="item.minRate" placeholder="请输入外放最低费率" suffix="%" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="外放最低笔数费">
                        <a-input v-model="item.minFee" placeholder="请输入外放最低笔数费" suffix="元/笔" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="成本费率">
                        <a-input v-model="item.costRate" placeholder="请输入成本费率" suffix="%" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="成本笔数费">
                        <a-input v-model="item.costFee" placeholder="请输入成本笔数费" suffix="元/笔" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="下单方式">
                        <a-select v-model="item.orderMode" placeholder="请选择下单方式" allowClear>
                            <a-select-option v-for="(item,i) in orderModeList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="是否压单">
                        <a-radio-group v-model="item.isWaitSubmit" >
                            <a-radio  v-for="(item,i) in isWaitSubmitList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                    <a-form-item label="权重(越大排序越靠前)">
                        <a-input v-model="item.sort" placeholder="请输入渠道权重(越大排序越靠前)" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="状态">
                        <a-radio-group v-model="item.status" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryExternalPaymentChannel , updateExternalPaymentChannel } from '@/api/product';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            channelName: '', // 渠道名称
            channelTag: '', // 渠道标识
            companyName: '', // 公司名称
            bankName: '', // 银行名称
            account: '', // 账号
            openingBank: '', // 开户行
            status: undefined,
            statusList: [
                { id: 1, name: '可用' },
                { id: 0, name: '关闭' },
            ],
            columns,
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            confirmLoading: false,
            isAdd: true,
            visible: false,
            classificationId: undefined, // 分类ID
            classificationList: [],
            editionList: this.global.editionList,
            operatorList: this.global.operatorList,
            orderMode: undefined,
            orderModeList: [
                { id: '0', name: 'API'},
                { id: '1', name: '手动'},
            ],
            isWaitSubmitList: [
                { id: 0, name: '自动提单'},
                { id: 1, name: '压单'},
            ],
            userType: sessionStorage.getItem('userType'), // 0：总平台，1：代理，2：商户
            item: {
                'id': '',
                'companyName': '', // 公司名称
                'channelTag': '', // 渠道标识
                'bankName': '', // 银行名称
                'account': '', // 账号
                'openingBank': '', // 开户行
                'minRate': '', // 外放最低费率
                'minFee': '', // 外放最低笔数费
                'costRate': '', // 成本费率
                'costFee': '', // 成本笔数费
                'sort': '', // 排序
                'orderMode': undefined, // 下单方式
                'status': '', // 状态，1：可用，0：不可用
                'isWaitSubmit': '0', // 是否压单
            },
            realTimeAmount: undefined,
            realTimeAmountList: [
                { id: 0, name: '不同步'},
                { id: 1, name: '同步'},
            ],
        };
    },
    async created(){
        try {
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 查询渠道配置
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'channelName':_this.channelName,
                'channelTag':_this.channelTag,
                'companyName': _this.companyName,
                'bankName': _this.bankName,
                'account': _this.account,
                'openingBank': _this.openingBank,
                'brandId': _this.global.brandId,
                'orderMode': _this.orderMode,
                'status':_this.status,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryExternalPaymentChannel(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true;
            this.item.id = '';
            this.item.companyName = '';
            this.item.channelTag = '';
            this.item.bankName = '';
            this.item.account = '';
            this.item.openingBank = '';
            this.item.minRate = '';
            this.item.minFee = '';
            this.item.costRate = '';
            this.item.costFee = '';
            this.item.orderMode = '0';
            this.item.status = 1
            this.item.isWaitSubmit = 0
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.item.minRate = (item.minRate * 100).toFixed(2);
            this.item.costRate = (item.costRate * 100).toFixed(2);
            this.isAdd = false;
            this.visible = true
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            if(_this.item.companyName == ''){
                _this.$message.warning('请输入公司名称')
                return;
            }
            if(_this.item.channelTag == ''){
                _this.$message.warning('请输入渠道标识')
                return;
            }
            if(_this.item.bankName == ''){
                _this.$message.warning('请输入银行名称')
                return;
            }
            if(_this.item.account == ''){
                _this.$message.warning('请输入账户信息')
                return;
            }
            if(_this.item.openingBank == ''){
                _this.$message.warning('请输入开户行')
                return;
            }
            if(_this.item.minRate == ''){
                _this.$message.warning('请输入外放最低费率')
                return;
            }
            if(_this.item.minFee == ''){
                _this.$message.warning('请输入外放最低笔数费')
                return;
            }
            if(_this.item.costRate == ''){
                _this.$message.warning('请输入成本费率')
                return;
            }
            if(_this.item.costFee == ''){
                _this.$message.warning('请输入成本笔数费')
                return;
            }
            if(isNaN(_this.item.sort)){
                _this.$message.warning('权重必须为纯数字')
                return;
            }
            if(_this.item.orderMode === undefined){
                _this.$message.warning('请选择下单方式')
                return;
            }
            const params = {
                'id': _this.item.id,
                'companyName': _this.item.companyName,
                'channelTag': _this.item.channelTag,
                'bankName': _this.item.bankName,
                'account': _this.item.account,
                'openingBank': _this.item.openingBank,
                'minRate': _this.item.minRate / 100,
                'minFee': _this.item.minFee,
                'costRate': _this.item.costRate / 100,
                'costFee': _this.item.costFee,
                'orderMode': _this.item.orderMode,
                'brandId': _this.global.brandId,
                'sort': _this.item.sort,
                'isWaitSubmit': _this.item.isWaitSubmit,
                'status': _this.item.status,
            }
            updateExternalPaymentChannel(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false;
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id': item.id,
                'isDelete': '1'
            }
            updateExternalPaymentChannel(params)
            .then(res=>{
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>
</style>